package com.handpay.nocardpay.baselibrary.main.mine.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.mine.model.bean.DayLimit;
import com.handpay.nocardpay.baselibrary.main.mine.model.bean.MerchInfo;
import com.handpay.nocardpay.baselibrary.main.mine.presenter.MinePresenterImpl;
import com.handpay.nocardpay.baselibrary.main.mine.view.ILimitView;
import com.handpay.nocardpay.baselibrary.main.mine.view.IRefreshMerchInfoListener;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/13.
 */

public class YesQualificationFragment extends BaseFragment implements ILimitView, IRefreshMerchInfoListener {

    private TextView tv_version;
    private TextView tv_username;
    private TextView tv_money;

    private MinePresenterImpl minePresenter;
    private String userName;

    public static YesQualificationFragment newInstance() {

        Bundle args = new Bundle();

        YesQualificationFragment fragment = new YesQualificationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_yes_qualification;
    }

    @Override
    public void initView(View view) {
        tv_money = (TextView) view.findViewById(R.id.tv_money);
        tv_username = (TextView) view.findViewById(R.id.tv_username);
        tv_version = (TextView) view.findViewById(R.id.tv_version);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        HPLog.d(HPLog.SSX_TAG, "onSupportVisible");
        userName = AccountUtils.getUserName(_mActivity);
        HPLog.d(HPLog.SSX_TAG, "userName:" + userName);
        tv_username.setText(String.format(_mActivity.getString(R.string.mine_user), userName));
        queryLimit();
    }

    /**
     * 查询今日收款
     */
    private void queryLimit() {
        HashMap<String, Object> paramers = new HashMap<>();
        HPLog.d(HPLog.SSX_TAG, "phone:" + AccountUtils.getAccountPhone(_mActivity));
        HPLog.d(HPLog.SSX_TAG, "accountName:" + AccountUtils.getAccountName(_mActivity));
        paramers.put(BaseProtocolKey.PHONE, SecureManager.getInstance().des(AccountUtils.getAccountPhone(_mActivity), 1, null));
        paramers.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(_mActivity), 1, null));
        paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        paramers.put(BaseProtocolKey.APPCODE, BaseProtocolKey.APPCODE_TRANSFER);
        if (minePresenter != null) {
            minePresenter.queryLimit(paramers, false);
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void querySuccess(DayLimit dayLimit) {
        tv_money.setText(String.format(_mActivity.getString(R.string.mine_money), CommonUtils.formatMoney(Double.parseDouble(dayLimit.getDayTraffic()) / 100)));
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        minePresenter = new MinePresenterImpl(_mActivity);
        minePresenter.setmLimitView(this);
        tv_money.setText(String.format(_mActivity.getString(R.string.mine_money), CommonUtils.formatMoney(0d)));
        tv_version.setText(String.format(_mActivity.getString(R.string.mine_version), ClientEngine.getInstance().VERSION));
        tv_username.setText(String.format(_mActivity.getString(R.string.mine_user), userName));

        ((MineFragment) getParentFragment()).setMerchInfoListener(this);
    }

    @Override
    public void refreshMerchInfo(MerchInfo merchInfo) {
        userName = merchInfo.getName();
        HPLog.d(HPLog.SSX_TAG, "userName:" + userName);
        tv_username.setText(String.format(_mActivity.getString(R.string.mine_user), userName));
    }
}
